package com.ycy.smartcourseselection.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycy.smartcourseselection.common.ResCodeEnum;
import com.ycy.smartcourseselection.common.ResData;
import com.ycy.smartcourseselection.dto.courseManage.CoManagesDTO;
import com.ycy.smartcourseselection.entity.CoManage;
import com.ycy.smartcourseselection.entity.Course;
import com.ycy.smartcourseselection.exception.CustomException;
import com.ycy.smartcourseselection.mapper.ICoManageMapper;
import com.ycy.smartcourseselection.utils.CommonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Service
public class CoManageService extends ServiceImpl<ICoManageMapper, CoManage> {
    @Resource
    CourseService courseService;

    //判断课程是否正在选课
    public ResData<Boolean> judgeCoIsBegin(String courseId) {
        if(StrUtil.isBlank(courseId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"课程id不能为空");

        //判断课程是否存在
        // judgeCoIsExist(courseId);
        // List<CoManage> coManageList = judgeCoManageIsExist(courseId);
        QueryWrapper<CoManage> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<CoManage> coManageList = list(wrapper);
        if(coManageList.size() == 0) return ResData.success(false);  //当不存在时，默认为未开始
        if(coManageList.size() > 1) throw new CustomException(ResCodeEnum.BAD_REQUEST,"错误地存在多个该课程管理信息");

        String beginTime = coManageList.get(0).getBeginTime();
        String endTime = coManageList.get(0).getEndTime();
        // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
        // String nowTime = sdf.format(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        String nowTime = sdf.format(new Date());
        if(CommonUtils.compareDate(nowTime, beginTime) > 0 && CommonUtils.compareDate(nowTime, endTime) < 0) return ResData.success(true);
        else return ResData.success(false);
    }

    //修改选课管理(因为添加已经在添加课程时一起添加了)
    public ResData<Boolean> saveCoManage(CoManage coManage) {
        // boolean isAdd = StrUtil.isBlank(coManage.getId());
        String courseId = coManage.getCourseId();
        String beginTime = coManage.getBeginTime();
        String endTime = coManage.getEndTime();

        if(StrUtil.isBlank(courseId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"课程id不能为空");
        if(StrUtil.isBlank(beginTime)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"开始时间为空");
        if(StrUtil.isBlank(endTime)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"结束时间不能为空");

        //判断课程是否存在
        // judgeCoIsExist(courseId);

        QueryWrapper<CoManage> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        boolean isRemove = remove(wrapper);
        // if(!isRemove) throw new CustomException(ResCodeEnum.BAD_REQUEST,"清空记录失败");

        return ResData.success(saveOrUpdate(coManage));
    }

    //批量开始选课
    public ResData<Boolean> saveBatchCoManage(CoManagesDTO coManagesDTO) {
        // boolean isAdd = StrUtil.isBlank(coManage.getId());
        ArrayList<String> courseIdList = coManagesDTO.getCourseIdList();
        String beginTime = coManagesDTO.getBeginTime();
        String endTime = coManagesDTO.getEndTime();

        if(courseIdList.size() == 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"课程id数组不能为空");
        if(StrUtil.isBlank(beginTime)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"开始时间为空");
        if(StrUtil.isBlank(endTime)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"结束时间不能为空");

        //判断课程是否存在
        // judgeCoIsExist(courseId);

        QueryWrapper<CoManage> wrapper = new QueryWrapper<>();
        wrapper.in("course_id",courseIdList);
        List<CoManage> list = list(wrapper);
        boolean isRemove = remove(wrapper);

        ArrayList<CoManage> addList = new ArrayList<>();
        for (String courseId: courseIdList) {
            CoManage coManage = new CoManage();
            coManage.setCourseId(courseId);
            coManage.setBeginTime(beginTime);
            coManage.setEndTime(endTime);
            addList.add(coManage);
        }

        return ResData.success(saveOrUpdateBatch(addList));
    }


    //工具方法  ---------------------------------------------------------
    private void judgeCoIsExist(String courseId) {
        QueryWrapper<Course> courseWrapper = new QueryWrapper<>();
        courseWrapper.eq("id",courseId);
        List<Course> courseList = courseService.list(courseWrapper);
        if(courseList.size() == 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"该课程不存在");
        if(courseList.size() > 1) throw new CustomException(ResCodeEnum.BAD_REQUEST,"错误地存在多个该课程信息");
        // return courseList;
    }
    // private List<CoManage> judgeCoManageIsExist(String courseId) {
    //     QueryWrapper<CoManage> wrapper = new QueryWrapper<>();
    //     wrapper.eq("course_id",courseId);
    //     List<CoManage> list = list(wrapper);
    //     if(list.size() == 0) throw new CustomException(ResCodeEnum.BAD_REQUEST,"该课程管理不存在");
    //     if(list.size() > 1) throw new CustomException(ResCodeEnum.BAD_REQUEST,"错误地存在多个该课程管理信息");
    //     return list;
    // }
}
